package org.abluedog.ssfb.commands;

import org.abluedog.ssfb.StartSurvivalFromBaby;
import org.abluedog.ssfb.pdc.BodyComposition;
import org.abluedog.ssfb.utils.SsfbUtil;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class SSFBCommands implements TabExecutor {
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (args.length == 0) {
            sender.sendMessage("§b§lStartSurvivalFromBaby§r§f v1.0 input /ssfb help to get more commands");
        } else {
            String firstArg = args[0];
            String lowerCase = firstArg.toLowerCase();
            switch (lowerCase) {
                case "help":
                    sender.sendMessage("§6§l======= 帮助菜单 =======");
                    sender.sendMessage("§e/ssfb getinformation | <playerName>");
                    sender.sendMessage("§e/ssfb scoreboard");
                    sender.sendMessage("§6§l=====================");
                    break;
                case "getinformation":
                    if (args.length > 1) {
                        String playerName = args[1];
                        Player playerExact = Bukkit.getServer().getPlayerExact(playerName);
                        if (playerExact == null) {
                            sender.sendMessage("§c§lPlayer §r§f" + playerName + "§c§l is not online§r§f");
                            return true;
                        }
                        BodyComposition bodyCompositionByPlayer = SsfbUtil.getBodyCompositionByPlayer(playerExact, StartSurvivalFromBaby.getPlugin());
                        SsfbUtil.sendBodyCompositionMsg(playerExact, bodyCompositionByPlayer);
                    } else {
                        if (sender instanceof Player player) {
                            BodyComposition bodyCompositionByPlayer = SsfbUtil.getBodyCompositionByPlayer(player, StartSurvivalFromBaby.getPlugin());
                            SsfbUtil.sendBodyCompositionMsg(player, bodyCompositionByPlayer);
                        } else {
                            sender.sendMessage("§c§lYou are not a player§r§f");
                        }
                    }
                    break;
                case "scoreboard":
                    if (sender instanceof Player player) {
                        BodyComposition bodyCompositionByPlayer = SsfbUtil.getBodyCompositionByPlayer(player, StartSurvivalFromBaby.getPlugin());
                        ScoreboardManager scoreboardManager = StartSurvivalFromBaby.getPlugin().getServer().getScoreboardManager();
                        assert scoreboardManager != null;
                        Scoreboard newScoreboard = scoreboardManager.getNewScoreboard();
                        Objective objective = newScoreboard.registerNewObjective("test", Criteria.DUMMY, ChatColor.YELLOW + "body-composition");
                        objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                        Score score = objective.getScore("age:" + bodyCompositionByPlayer.getAge());
                        score.setScore(8);
                        Score score1 = objective.getScore("height:" + bodyCompositionByPlayer.getHeight());
                        score1.setScore(7);
                        Score score2 = objective.getScore("weight:" + bodyCompositionByPlayer.getWeight());
                        score2.setScore(6);
                        Score score3 = objective.getScore("bmr:" + bodyCompositionByPlayer.getBmr());
                        score3.setScore(5);
                        Score score4 = objective.getScore("proteinMass:" + bodyCompositionByPlayer.getProteinMass());
                        score4.setScore(4);
                        Score score5 = objective.getScore("muscleMass:" + bodyCompositionByPlayer.getMuscleMass());
                        score5.setScore(3);
                        Score score6 = objective.getScore("fatMass:" + bodyCompositionByPlayer.getFatMass());
                        score6.setScore(2);
                        Score score7 = objective.getScore("waterMass:" + bodyCompositionByPlayer.getWaterMass());
                        score7.setScore(1);
                        player.setScoreboard(newScoreboard);
                    } else {
                        sender.sendMessage("§c§lYou are not a player§r§f");
                    }
                break;
                    default:
                        sender.sendMessage("§b§lStartSurvivalFromBaby§r§f v1.0 input /ssfb help to get more commands");
                        break;
            }
        }

        return true;
    }

    @Nullable
    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (args.length == 1) {
            String firstArg = args[0];
            String lowerCase = firstArg.toLowerCase();
            switch (lowerCase) {
                case "help":
                    sender.sendMessage("§6§l======= 帮助菜单 =======");
                    sender.sendMessage("§e/ssfb info | <playerName> *get bodyComposition from playerName,if no playerName parameter It will get yourself*");
                    sender.sendMessage("§e/ssfb scoreboard | sc *open the scoreboard,again input to open or close it*");
                    sender.sendMessage("§6§l=====================");
                    break;
                case "info":
                    return Bukkit.getServer().getOnlinePlayers().stream().map(Player::getName).toList();
                case "scoreboard","sc":
                    break;
                default:
                    sender.sendMessage("§b§lStartSurvivalFromBaby§r§f v1.0 input /ssfb help to get more commands");
                    return List.of();
            }
        }
        return List.of("help", "info", "scoreboard");
    }
}
